using System;
using System.Collections.Generic;
using System.Linq;

namespace CF_DAL_WS.Repositories
{
	#region using
	using CF_DAL_WS.Models;
	using CF_DAL_WS.Helpers;
	#endregion
	public class RestaurantRepository
	{
		public List<Restaurant> SelectAll()
		{
			try
			{
                return DataContext.Instance.Restaurant.OrderBy(n => n.Name).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Restaurant>();
		}

		public Restaurant SelectByID(int id)
		{
			try
			{
				return DataContext.Instance.Restaurant.FirstOrDefault(item => item.ID == id );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public bool Insert(Restaurant obj)
		{
			try
			{
				DataContext.Instance.Restaurant.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.Restaurant.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.Restaurant.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public List<Restaurant> RetrieveByID(int id)
		{
			try
			{
				return (from item in DataContext.Instance.Restaurant where  item.ID == id  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Restaurant>();
		}

		public List<Restaurant> SelectByIDDistrict(int iddistrict)
		{
			try
			{
                var lstItem = (from item in DataContext.Instance.Restaurant where item.IDDistrict == iddistrict select item).OrderBy(n => n.Name).ToList();
				return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Restaurant>();
		}

		public List<Restaurant> SelectByIDFoodType(int idfoodtype)
		{
			try
			{
                var lstItem = (from item in DataContext.Instance.Restaurant where item.IDFoodType == idfoodtype select item).OrderBy(n => n.Name).ToList();
				return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<Restaurant>();
		}

        public List<Restaurant> SelectByIDDistrictAndIDFoodType(int iddistrict, int idfoodtype)
        {
            try
            {
                var lstItem = (from item in DataContext.Instance.Restaurant where item.IDFoodType == idfoodtype && item.IDDistrict == iddistrict select item).OrderBy(n => n.Name).ToList();
                return lstItem;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return new List<Restaurant>();
        }
	}
}